﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Cofe.Core.Interfaces;
using Cofe.Core.Property;

namespace Cofe.Core.Actions
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
    public class LinkEntryTransfer : ITransferHelper
    {

        public TransferMode GetSupportedMode(ICofeSystemInfo[] entries, ICofeDirectoryInfo fromm, ICofeDirectoryInfo too)
        {
            TransferMode retVal = TransferMode.None;

            if (too.Properties.Behaviors.IsPropertySupported(CofeEntryLinkProperties.AddLink))
                retVal |= TransferMode.Link;

            if (entries.Any(e => e.Is<IEntryLink>()))
                return retVal = TransferMode.Copy | TransferMode.Link; //Copy links, not support link links.

            return retVal;
        }

        public async Task<TransferMode> TransferAsync(TransferMode mode, ICofeSystemInfo[] entries, ICofeDirectoryInfo fromm,
            ICofeDirectoryInfo too, IProgress<ProgressEventArgs> progress, CancellationToken cts)
        {
            if (entries == null)
                throw new ArgumentException("entry");

            if (too == null)
                throw new ArgumentException("too");

            await too.Properties.ConnectAsync();
            if ((mode == TransferMode.Link && GetSupportedMode(entries, fromm, too).HasFlag(TransferMode.Link)) ||
                (mode == TransferMode.Copy && GetSupportedMode(entries, fromm, too).HasFlag(TransferMode.Copy)))
            {
                foreach (var entry in entries)
                    if (!entry.Is<IEntryLink>())
                    {
                        too.Properties.Behaviors.Invoke<IPropertyHost>(CofeEntryLinkProperties.AddLink,
                            new ParameterDic() { { "ParseName", entry.ParseName }, { "Label", entry.Label } });
                    }
                    else
                    {
                        var ph = entry.Properties.Behaviors.GetProperty<IPropertyHost>(CofeEntryLinkProperties.LinkedEntryPH);
                        if (ph != null)
                        {
                            string parseName = ph.Behaviors.GetProperty<string>(CofeProperties.FullParseName);

                            too.Properties.Behaviors.Invoke<IPropertyHost>(CofeEntryLinkProperties.AddLink,
                                new ParameterDic() { { "ParseName", parseName }, { "Label", entry.Label } });
                        }
                    }

                return TransferMode.Link;
            }


            return TransferMode.None;

        }

        public Task<TransferMode> RenameAsync(TransferMode mode, ICofeSystemInfo entry, ICofeDirectoryInfo fromm,
         string newName, IProgress<ProgressEventArgs> progress, CancellationToken cts)
        {
            throw new NotImplementedException();
        }



        private IEntryConstructor _entryConstructor = null;
        public ITransferHelper[] _allHelpers = null;

        public IEntryConstructor EntryConstructor
        {
            get { return _entryConstructor ?? CofeServices.ServiceLocater.FindService<IEntryConstructor>(); }
            set { _entryConstructor = value; }
        }
        public ITransferHelper[] AllHelpers
        {
            get { return _allHelpers ?? CofeServices.ServiceLocater.FindAllServices<ITransferHelper>().ToArray(); }
            set { _allHelpers = value; }
        }
    }
}
